package borge.web.teamspirit.client.widgets;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class CreateMatchWidget extends CreateDataWidget implements ClickHandler {

    private Match iMatch;
    
    public CreateMatchWidget(WidgetEventHandler aEventHandler, Match aMatch){
        super(aEventHandler);
        
        iMatch = aMatch;
        // Need an "empty" instance if not editing
        if(aMatch == null)
            aMatch = new Match();
        
        int row = 0;
        final int maxLength = 30;
        TeamSpiritConstants lcs = TeamSpirit.getConstants();
        
        // We want to populate a list with series to choose from, newest first in list
        // Let item text be Season Division, and value be serie id
        
        addTableRow(row++,new InputWrapper(lcs.opponents(), createTextBox(aMatch.getOpponents(), maxLength), true));
        
        addTableRow(row++, new InputWrapper(lcs.date(), createDateBox(aMatch.getDateTime()), true));
        addTableRow(row++, new InputWrapper(lcs.gathering_time(), createDateBox(aMatch.getGatheringTime()), false));
        addTableRow(row++, new InputWrapper(lcs.city(), createTextBox(aMatch.getCity(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.stadium(), createTextBox(aMatch.getStadium(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.field(), createTextBox(aMatch.getField(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.periods(), createTextBox(aMatch.getPeriods(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.period_length(), createTextBox(aMatch.getPeriodLengthInMinutes(), maxLength), false));
        
        addTableRow(row++, new InputWrapper(lcs.our_score(), createTextBox(aMatch.getOurScore(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.opponent_score(), createTextBox(aMatch.getOpponentScore(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.cup_round(), createTextBox(aMatch.getCupRound(), maxLength), false));
        addTableRow(row++, new InputWrapper(lcs.other_info(), createTextArea(aMatch.getOtherInfo(), maxLength, 5), false));
        
        addButton(row, 0, this);
        iPanel.add(iTable);
        initWidget(iPanel);
    }
    
    @SuppressWarnings("unchecked")
    public Match getDataObject(){
        return iMatch;
    }
    
    public void onClick(ClickEvent aEvent){
        
        postEvent(WidgetEventType.MATCH_CREATED);
        
    }
}
